package codingforgreat.class02;

public class Class06_MinLengthForSort {
    public int findUnsortedSubarray(int[] nums) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        int minIndex = nums.length;
        int maxIndex = -1;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] < max){
                maxIndex = i;
            }
            max = Math.max(nums[i], max);

        }
        for (int i = nums.length - 1; i >= 0 ; i--) {
            if(nums[i] > min){
                minIndex = i;
            }
            min = Math.min(nums[i], min);
        }
        return Math.max(0, maxIndex - minIndex + 1);
    }
}
